package com.qf.zyh.web.filter;

import com.qf.zyh.common.Const;
import com.qf.zyh.entity.Emp;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: COS
 * @time: 2021/10/28 11:47
 * @description:
 */
@WebFilter(filterName = "Filter02_Role", value = "/*")
public class Filter2 implements Filter {
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        //1 转http的request和response
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        //2 检查session有没有登录信息
        Emp e = (Emp) request.getSession().getAttribute(Const.IS_LOGIN);
        //没有登录
        if (e==null) {
            String requestURI = request.getRequestURI();
            //忽略大小写，讲路径全部转大写,而且验证码也需要正常传输
            //检查路径上是否有log或code，有就放行
            if (requestURI.toUpperCase().contains("LOG")||requestURI.toUpperCase().contains("CODE")||requestURI.toUpperCase().contains("EMP")) {
                chain.doFilter(request,response);
            }else {
                response.sendRedirect(request.getContextPath() + "/login.jsp");
            }
        }else {
            chain.doFilter(request,response);
        }
            // requestURI.toUpperCase() 因为有部分路径可能有大小写问题出现,为了处理吧路径直接转全大写,忽略大小写
    }

    @Override
    public void init(FilterConfig config) throws ServletException {

    }

}
